package com.mezz.bones.framework.web.satoken;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.fun.SaParamFunction;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.hutool.extra.servlet.JakartaServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.mezz.bones.framework.base.domain.enums.ResponseCode;
import com.mezz.bones.framework.base.domain.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * @Author: zhuang
 * @Date: 2024/17/8 17:45
 * @Description: TODO
 * @Version: 1.0
 **/

public class CustomSaInterceptor extends SaInterceptor {

    public CustomSaInterceptor(SaParamFunction<Object> auth) {
        super.auth = auth;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        boolean flag = true;
        try {
            flag = super.preHandle(request, response, handler);

        }catch (NotLoginException e){
            JakartaServletUtil.write(response, JSONObject.toJSONString(Result.error(ResponseCode.USER_NOT_LOGIN)),"application/json;charset=utf-8");
            return false;
        }

        return flag;
    }
}
